package controllers

import (
	"Learn/base"
	"encoding/json"

	"github.com/kataras/iris"
)

type ResponseData struct {
	Success bool        `json:"success"`
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
	Msg     string      `json:"msg,omitempty"`
}

func ResponseSuccess(ctx iris.Context, data interface{}, code int, message string) bool {

	var resp = ResponseData{}
	resp.Success = true
	resp.Code = code
	resp.Data = data
	resp.Msg = message

	respByte, _ := json.Marshal(resp)

	ctx.ContentType("application/json")

	_, _ = ctx.Write(respByte)
	return true
}

func ResponseError(ctx iris.Context, error error, code int, message string) bool {
	base.App.Logger().Error(error)
	var resp = ResponseData{}
	resp.Success = false
	resp.Code = code
	resp.Data = error.Error()
	resp.Msg = message

	respByte, _ := json.Marshal(resp)

	ctx.ContentType("application/json")

	_, _ = ctx.Write(respByte)
	return true
}
